package com.nickstumpos.android.GmailWidgetPlus.service.IntentService;

import java.util.Calendar;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;

import com.nickstumpos.android.GmailWidgetPlus.Constants.GMWP_Constants;
import com.nickstumpos.android.GmailWidgetPlus.gmailProvider.GmailProvider;
import com.nickstumpos.android.GmailWidgetPlus.widget.sizes.WidgetSize;

public class GMWPIntentService {
	private final Context		context;
	private final String		account;
	private final WidgetSize	widgetSize;
	private final int			widgetID;

	public GMWPIntentService(final Context context,
			final String account,
			final int widgetID,
			final WidgetSize widgetSize) {
		super();
		this.context = context;
		this.account = account;
		this.widgetID = widgetID;
		this.widgetSize = widgetSize;
	}

	private int generateCode() {
		return (int) Calendar.getInstance().getTimeInMillis() << 4;
	}

	public PendingIntent newDummyIntent() {
		return PendingIntent.getActivity(this.context,
				this.generateCode(),
				new Intent(),
				0);
	}

	public PendingIntent newGMWPBroadcastIntent(final String action) {
		final Intent GMWPAction = new Intent();
		GMWPAction.setAction(this.widgetSize.getAction());
		GMWPAction.putExtra(GMWP_Constants.EXTRA_WIDGET_ACTION, action);
		GMWPAction.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, this.widgetID);
		return PendingIntent.getBroadcast(this.context,
				this.generateCode(),
				GMWPAction,
				0);
	}

	public PendingIntent openGmailLabelIntent(final String label) {
		final Intent openGmailLabel = GmailProvider.getGmailAvailable(this.context);
		openGmailLabel.putExtra(GMWP_Constants.EXTRA_GMAIL_LABEL, label);
		openGmailLabel.putExtra(GMWP_Constants.EXTRA_GMAIL_ACCOUNT,
				this.account);
		return PendingIntent.getActivity(this.context,
				this.generateCode(),
				openGmailLabel,
				0);
	}

	public PendingIntent openGmailSearchIntent(final String query) {
		final Intent openGmail = GmailProvider.getGmailAvailable(this.context);
		openGmail.setAction(GMWP_Constants.ACTION_GMAIL_SEARCH);
		openGmail.putExtra(GMWP_Constants.EXTRA_GMAIL_QUERY, query);
		openGmail.putExtra(GMWP_Constants.EXTRA_GMAIL_ACCOUNT, this.account);
		return PendingIntent.getActivity(this.context,
				this.generateCode(),
				openGmail,
				0);
	}
}
